package cn.tedu.api;

import java.util.Objects;

/*本类用于回顾Object常用方法的使用*/
public class TestObject2 {
    public static void main(String[] args) {
        Cat c1=new Cat();
        Cat c2=new Cat();
        Cat c3=new Cat("机器猫",300,10000.99,"大雄",'男');
        Cat c4=new Cat("机器猫",300,10000.99,"大雄",'男');
        Dog d1=new Dog();
        Dog d2=new Dog();
        Dog d3=new Dog("旺财",5,100);
        Dog d4=new Dog("旺财",5,100);
        System.out.println(c1);
        System.out.println(d2);
        System.out.println(d3);
        System.out.println(c3);
        //使用==比较底层逻辑的c3,c4的地址值
        System.out.println(c3==c4);
        System.out.println(c3.equals(c4));
        //重写后equals()不用==比较,不比较地址值,比较类型 属性 属性值
        System.out.println(d3==d4);//==永远比较的是引用类型保存的地址值
        System.out.println(d3.equals(d4));

    }
}
class Cat{
    String name;
    int age;
    double price;
    String host;
    char gender;
    public Cat(){
        System.out.println("无参构造");
    }

    public Cat(String name, int age, double price, String host, char gender) {
        this.name = name;
        this.age = age;
        this.price = price;
        this.host = host;
        this.gender = gender;
        System.out.println("全参构造");
    }
}
class Dog{
    String name;
    int age;
    double price;
    public Dog(){
        System.out.println("我是无参构造");
    }

    public Dog(String name, int age, double price) {
        this.name = name;
        this.age = age;
        this.price = price;
        System.out.println("我是全参构造");
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog dog = (Dog) o;
        return age == dog.age && Double.compare(dog.price, price) == 0 && Objects.equals(name, dog.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, price);
    }
}